package com.itheima.algorithm.recursion_multi;

public class E03PascalTriangle {
    private static int element(int [][] triangle,int i,int j){
        if(triangle[i][j]>0){
            return triangle[i][j];
        }
        if(j == 0 || i == j){
            triangle[i][j]=1;
            return 1;
        }
        return triangle[i][j]=element(triangle,i - 1,j - 1) + element(triangle,i - 1, j);
    }

    private static void printSpace(int n,int i){
        int num=(n-1-i)*2;
        for(int j=0;j<num;j++){
            System.out.print(" ");
        }
    }

    public static void print(int n){
        int [][] triangle=new int[n][];
        for(int i=0;i<n;i++){
            triangle[i]=new int[i+1];
            printSpace(n,i);
            for(int j=0;j<=i;j++){
                System.out.printf("%-4d",element(triangle,i,j));
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        print(10);
    }

    private static void createRow(int [] row,int i){//i代表行号
        //计算过就不用计算了
        if(row[i]>0){
            return;
        }
        if(i==0){
            row[0]=1;
            return;
        }
        //计算上一行
        createRow(row,i-1);
        for(int j=i;j>0;j--){
            row[j]=row[j]+row[j-1];
        }

    }
}
